
import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors } from '@nestjs/common';
import { ExebookService } from './exebook.service';
import { Exebook } from './entities/exebook.entity';
import { CreateExebookDto } from './dto/create-exebook.dto';
import { UpdateExebookDto } from './dto/update-exebook.dto';
import { ResultData } from 'src/common/response/result';
import { ResponsInterceptoreBase } from '../../../transform.interceptor';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { BaseController } from 'src/common/BaseController';
import { QueryListDto } from 'src/common/dto/';

@ApiTags('题库题型')
@Controller('exebook')
export class ExebookController extends BaseController<Exebook, ExebookService>{
  constructor(private readonly exebookService: ExebookService) {
    super(exebookService)
  }

  
  @Post('AddExebook')
  @ApiOperation({ summary: '添加题库题型' })
  async AddExebook(@Body() addexebook: CreateExebookDto) {
    return this.Success(await this.exebookService.AddExebook(addexebook), "添加成功!");
  }

  
  @Post('GetExebooks')
  @ApiOperation({ summary: '获取题库列表' })
  async GetExebooks(@Body() querylistDto: QueryListDto) {
    return this.Success(await this.exebookService.findAllbooks(querylistDto), "题库列表查询成功!");
  }

  @Get(':id')
  @ApiOperation({ summary: '根据id获取题目' })
  async findOne(@Param('id') id: string) {
    let result = await this.exebookService.findOnebook(id);
    
    if (result[0]) {
      return this.Success(result[1], "查询成功");
    } else {
      return this.Fail(result[1], "查询失败");
    }
  }

  // @Get(':questiontext') 刚开始这样写会报错，因为NestJS 会优先匹配第一个出现的 @Get(':xxx') 路由，也就是上面的id路由，这样导致路由没有起作用
  // 所以这里改成了 @Get('search/:questiontext'),换成了不同路径，这样就不会和id路由冲突了
  @Get('search/:questiontext')
  @ApiOperation({ summary: '根据题目内容获取题目' })
  async findOneBytext(@Param('questiontext') questiontext: string) {
    console.log('接收到的参数:', questiontext); // ← 这里应该打印
    let [result, data] = await this.exebookService.findOneBytext(questiontext);
    
    if (result && data) {
      return this.Success(data, '查询成功');
    } else {
      return this.Fail(null, '查询失败');
    }
  }


  @Patch("UpdateExebook")
  @ApiOperation({ summary: '题库题目更新' })
  async update(@Body() updateexebook: CreateExebookDto) {
    let result = await this.exebookService.updatebook(updateexebook);
    if(result[0]){
      return this.Success(result[1], "更新成功");
    }else{
      return this.Fail(result[1], "更新失败");
    }
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除指定题目' })
  remove(@Param('id') id: string) {
    return this.exebookService.removebook(id);
  }


}
